<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title>服务通知指派FSE</title>
		<!-- css代码 start -->
		<link href="../../../css/mui.min.css" rel="stylesheet" />
		<link href="../../../css/global.css" rel="stylesheet" />
		<link href="../../../css/style.css" rel="stylesheet" />
		<link href="../../../css/checkbox.css" rel="stylesheet" />
		<link href="../../../css/task.css" rel="stylesheet" />
		<style type="text/css">
			#div {
				width: 0px;
				height: 0px;
				background: red;
				position: fixed;
				top: 70%;
				left: 50%;
			}

			/*移除底部或顶部三角,需要在删除此代码*/
			.mui-popover .mui-popover-arrow:after {
				width: 0px;
			}

			#div1 {
				width: 0px;
				height: 0px;
				background: red;
				position: fixed;
				top: 70%;
				left: 50%;
			}

			/*移除底部或顶部三角,需要在删除此代码*/
			.mui-popover1 .mui-popover-arrow:after {
				width: 0px;
			}

			/* 待指派列表 */
			.zp_list .clum {
				height: auto;
				line-height: 24px;
				overflow: hidden;
			}

			.zp_list .clum .dan {
				float: right;
				width: 80px;
				text-align: right;
				line-height: 24px;
				color: #ff0000;
				font-size: 14px;
			}

			.zp_list .clum .address {
				margin-right: 0;
			}

			.popover .textArea {
				font-size: 14px;
			}

			.details_con {
				background: #fff;
			}

			input.d_ipt {
				width: 60%;
				background: #fff;
			}

			.mui-btn-block {
				border: 0;
				font-size: 16px;
				color: #a9a9a9;
			}
			.zp_cen li{
				height: auto;
			}
			.jjly .txt{
				width: 100% !important;
				font-size: 14px;
				line-height: 24px;
			}
			.zp_cen li .name{
				width: auto;
			}
		</style>
		<!-- css代码 end -->
	</head>
	<body>

		<div id="div"></div>
		<div id="popover" class="mui-popover shadow popover">
			<p class="title">此仪器没有设置Account FSE，是否将指派的FSE设置为Account FSE?</p>
			<div class="textArea" id="zhiding">

				<!-- <input class="serviceRole" name="serviceRole" type="radio" value="1" >张三 -->
			</div>
			<div class="btn_box">
				
				<span class="fl" onclick="closepop()">否</span>
				<span class="fr red" id="zhidingFse">是</span>
			</div>
		</div>

		<div id="div1"></div>
		<div id="popover1" class="mui-popover shadow popover">
			<p class="title" id="title">确定要删除？</p>
			<div class="btn_box">
				
				<span class="fl" onclick="closepop1()">取消</span>
				<span class="fr red" id="quedingupdate">确定</span>
			</div>
		</div>

		<!-- html代码 start -->
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">服务通知指派FSE</h1>
		</header>

		<div class="mui-content">
			<div class="task_tit"><span></span>已指派FSE列表</div>
			<div id="yipaidan">
				
			<!-- 	<ul class="page_cen shadow zp_cen">
				
					<li>
					<div><span class="state"></span>
					<span class="name">测试-页2(Rory)</span>
					<img src="../../../images/icon_42.png" />
					<span style="color:red" onclick="shanchu(\'' + fseId + '\')" >删除</span></div>
				
						<div class="jjly">
							<span class="txt">
						<span style="color:#000;">拒绝理由：</span>临时有事去不了</span></div>
				
					</li>
				
				</ul> -->
				
				
			<!-- 	<ul class="page_cen shadow zp_cen">
					<div>
						<span class="state">已拒绝</span>
						<span class="name">测试-丁1</span>
						<img src="../../../images/icon_42.png" />
						<span style="color:red" onclick="shanchu('10049')">删除</span>
					</div>
					<div class="jjly">
						<span class="txt">时间冲突去不了,立体蝴蝶结的就到九点半就到九点半就到家发吧哦下班吃吧就到此处不亏新剧场版次第出白皙</span>
					</div>
					</li>
				</ul> -->

				<!-- <ul class="page_cen shadow zp_cen">
					<li>
						<span class="state">待接受</span>
						<span class="name">张三</span>
						<span class="txt">此客户需要高工</span>
						<img src="../../../images/icon_42.png" />
					</li>
					<li>
						<span class="state">待接受</span>
						张三<img src="../../../images/icon_42.png" />
					</li>
					<li>
						<span class="state">待接受</span>
						张三<img src="../../../images/icon_42.png" />
					</li>
				</ul> -->
			</div>

			<div class="task_tit"><span></span>待指派FSE列表</div>
			<div class="page_cen shadow zp_cen">
				<div class="zp_sear autoHeight">
					<button class="btn" id="chaxun" onclick="getToBeAppointedFse()"></button>
					<div class="iptBox">
						<input type="text" id="fsename" placeholder="请输入工程师名称" />
					</div>
				</div>
				<div id="daizhipai">
					<!-- <div class="zp_list autoHeight">
						<div class="clum">
							<div class="mui-input-row mui-checkbox zp_check mui-left">
							    <input name="checkbox1" value="Item 1" type="checkbox">
								<label>张三</label>
							</div>
							<span class="num">6</span>
						</div>
						<div class="clum">
							<span class="dan">2.1KM</span>
							<span class="address">上海市长宁区延安西路1326号</span>
						</div>
					</div>
					<div class="zp_list autoHeight">
						<div class="clum">
							<div class="mui-input-row mui-checkbox zp_check mui-left">
							    <input name="checkbox1" value="Item 1" type="checkbox">
								<label>张三</label>
							</div>
							<span class="num">6</span>
						</div>
						<div class="clum">
							<span class="dan">2.1KM</span>
							<span class="address">上海市长宁区延安西路1326号</span>
						</div>
					</div> -->
				</div>

			</div>
			<div class="btn_box" style="overflow: auto;">
				<span class="fl red one" id="quedingpaigong">确认派工</span>
			</div>
			<div style="height: 100px;"></div>
		</div>
		<!-- html代码 end -->

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/utils/h.js"></script>
		<script src="../../../js/constant.js"></script>
		<script src="../../../js/common.js"></script>
		<script src="../../../js/utils/base64.js"></script>
		<script src="../../../js/utils/md5.js"></script>
		<script src="../../../js/utils/sortMap.js"></script>
		<script src="../../../js/utils/http.js"></script>
		<script src="../../../js/app.js"></script>
		<!-- 基础js end -->

		<script type="text/javascript" charset="utf-8">
			var ServiceNoticeId;
			var serviceNo;
			var serviceType;
			var isWarranty;
			var fseIds = "";
			var fseNames = "";
			var fseId;

			// 指定FSE弹窗页面方法
			function openpop() {
				var html = "";
				h("#zhiding").html("");
				if (fseIds != "") {
					var fseIDS = fseIds.split(",");
					var fseNAMES = fseNames.split(",");
					for (var i = 0; i < fseIDS.length; i++) {
						if (fseIDS[i] != "") {
							html += '<span id="' + fseIDS[i] + '1" fname=' + fseNAMES[i] + ' style="margin-right:10px;">'
							html += '<input class="accoutfseId" name="accoutfseId" type="radio" value="' + fseIDS[i] + '">' + fseNAMES[i]
							html += '</span>&nbsp;&nbsp;';
						}
					}
					h("#zhiding").html(html);
				}
				mui("#popover").popover('toggle', document.getElementById("div"));
			}

			// 关闭弹窗方法
			function closepop() {
				// 调用确定派工方法
				app.showWaiting('正在提交...')
				quedingpg();
			}

			// 关闭弹窗方法
			function closepop1() {
				mui("#popover1").popover('toggle', document.getElementById("div1"));
			}

			(function($, doc) {
				$.init();

				$.plusReady(function() {
					// 从上一个页面取值
					var self = plus.webview.currentWebview();
					// 获取上一个页面传过来的参数并显示到页面
					ServiceNoticeId = self.ServiceNoticeId;
					serviceNo = self.serviceNo;
					isWarranty = self.isWarranty;
					serviceType = self.serviceType;

					// 获取该服务单已派单的FSE列表信息接口
					getSentFseList()

					// 获取该服务单已派单的FSE列表信息接口
					getToBeAppointedFse();

					// 指定FSE方法
					h("#zhidingFse").tap(function() {
						app.showWaiting('正在提交...')
						var accoutfseId = "";
						var AccoutfseName = "";
						var ele = document.getElementsByName("accoutfseId");
						for (var i = 0; i < ele.length; i++) {
							if (ele[i].checked) {
								accoutfseId = ele[i].value
								AccoutfseName = document.getElementById(ele[i].value + "1").getAttribute('fname')
								break
							}
						}

						if (accoutfseId == "") {
							app.closeWaiting()
							app.toast("请选择要指定的Account FSE")
							return
						}

						ajaxGet(BASE_URL + "cc/instrument/setPartAccountFSE", {
							"ServiceNoticeId": ServiceNoticeId,
							"ServiceNo": serviceNo,
							"TeamleaderId": app.getUser().enginnerId,
							"AccoutfseId": accoutfseId,
							"AccoutfseName": AccoutfseName
						}, function(data) {
							if (data.code == 200) {
								// app.toast("操作成功");

								// 调用确定派工方法
								quedingpg();

							} else {
								app.closeWaiting()
								app.toast("此服务单数据异常（没有仪器详细信息），暂无法指定AccountFSE");
								mui("#popover").popover('hide', document.getElementById("div"));
								return
							}
						}, function(errorMsg) {
							app.closeWaiting()
							console.log(JSON.stringify(errorMsg))
						});

					});

					// 确定删除方法
					h("#quedingupdate").tap(function() {
						app.showWaiting('正在提交...')
						ajaxGet(BASE_URL + "cc/ServiceDetails/deleteServiceDetails", {
							"serviceNoticeId": ServiceNoticeId,
							"enginnerId": fseId,
							"sendenginnerId": app.getUser().enginnerId
						}, function(data) {
							app.closeWaiting()
							mui("#popover1").popover('hide', document.getElementById("div1"));
							console.log(JSON.stringify(data))
							if (data.code == 200) {
								app.toast("操作成功")
								var assignFSE = plus.webview.getWebviewById('view/service/assign/assignFSE.html');
								if (assignFSE) {
									assignFSE.evalJS('getSentFseList()')
									assignFSE.evalJS('getToBeAppointedFse()')
								}
							} else {
								app.toast(data.msg);
								mui("#popover1").popover('hide', document.getElementById("div1"));
								return
							}
						}, function(errorMsg) {
							app.closeWaiting()
							console.log(JSON.stringify(errorMsg))
						});

					})

					// 确定派工方法
					h("#quedingpaigong").tap(function() {
						app.showWaiting('正在提交...')
						// 获取checkbox的值
						var ele = document.getElementsByName("checkbox1");
						fseIds = "";
						fseNames = "";
						for (var i = 0; i < ele.length; i++) {
							if (ele[i].checked) {
								fseIds += ele[i].value + ",";
								fseNames += document.getElementById(ele[i].value).getAttribute('fseName') + ",";
							}
						}

						if (fseIds == "") {
							app.closeWaiting()
							app.toast("请选择待指派人员");
							return
						}
						
						if (serviceType === "安装" || isWarranty === "保内" || isWarranty === "服务合同") {
							// 验证仪器是否指定FSE 没指定弹窗提示指定
							checkAccoutFse(function(flag) {
								if (!flag) {
									app.closeWaiting()
									// 调用指定FSE弹窗
									openpop()
								} else {
									ajaxGet(BASE_URL + "cc/notice/ConfirmationDispatch", {
										"assignId": app.getUser().enginnerId,
										"ServiceNoticeId": ServiceNoticeId,
										"serviceNo": serviceNo,
										"EnginnerIds": fseIds,
										"EnginnerNames": fseNames
									}, function(data) {
										app.closeWaiting()
										if (data.code == 200) {
											app.toast("操作成功");
											// 查找上级页面并关闭页面
											var details = plus.webview.getWebviewById('view/service/assign/details.html');
											if (details) {
												details.close()
											}
											// 查找需要跳转的页面，并指定刷新的方法
											var teamList = plus.webview.getWebviewById('view/service/notice/teamList.html');
											var assignList = plus.webview.getWebviewById('view/service/assign/assignList.html');
											if (teamList) {
												teamList.evalJS('initData()')
												$.back()
											} else if (assignList) {
												assignList.evalJS('getList()')
												$.back()
											} else {
												var assignList1 = plus.webview.create('/view/service/assign/assignList.html',
													'view/service/assign/assignList.html')
												assignList1.show()
												self.close()
											}
										} else {
											app.toast("操作失败");
										}
									}, function(errorMsg) {
										app.closeWaiting()
										console.log(JSON.stringify(errorMsg))
									});
								}
							})
						} else {
							ajaxGet(BASE_URL + "cc/notice/ConfirmationDispatch", {
								"assignId": app.getUser().enginnerId,
								"ServiceNoticeId": ServiceNoticeId,
								"serviceNo": serviceNo,
								"EnginnerIds": fseIds,
								"EnginnerNames": fseNames
							}, function(data) {
								app.closeWaiting()
								if (data.code == 200) {
									app.toast("操作成功");
									// 查找上级页面并关闭页面
									var details = plus.webview.getWebviewById('view/service/assign/details.html');
									if (details) {
										details.close()
									}
									// 查找需要跳转的页面，并指定刷新的方法
									var teamList = plus.webview.getWebviewById('view/service/notice/teamList.html');
									var assignList = plus.webview.getWebviewById('view/service/assign/assignList.html');
									if (teamList) {
										teamList.evalJS('initData()')
										$.back()
									} else if (assignList) {
										assignList.evalJS('getList()')
										$.back()
									} else {
										var assignList1 = plus.webview.create('/view/service/assign/assignList.html',
											'view/service/assign/assignList.html')
										assignList1.show()
										self.close()
									}
								} else {
									app.toast("操作失败");
								}
							}, function(errorMsg) {
								app.closeWaiting()
								console.log(JSON.stringify(errorMsg))
							});
						}
					});
				});
			}(mui, document));

			// 验证是否指定过FSE
			function checkAccoutFse(callback) {
				ajaxGet(BASE_URL + "cc/instrument/checkAccoutFse", {
					"ServiceNoticeId": ServiceNoticeId
				}, function(data) {
					console.log(JSON.stringify(data))
					if (data.code == 200) {
						callback(data.data)
					}
				}, function(errorMsg) {
					app.closeWaiting()
					console.log(JSON.stringify(errorMsg))
				});
			}

			// 获取该服务单已派单的FSE列表信息接口
			function getSentFseList() {
				ajaxGet(BASE_URL + "cc/location/getSentFseList", {
					"ServiceNoticeId": ServiceNoticeId
				}, function(data) {
					if (data.code == 200) {
						var html = "";
						h("#yipaidan").html("");
						if (data.data != '' && data.data[0].fseName != undefined) {
							
							html += '<ul class="page_cen shadow zp_cen">';
							for (var i = 0; i < data.data.length; i++) {
								var dts = data.data[i];
								var state = dts.isAccept == undefined ? "" : dts.isAccept;
								var fseName = dts.fseName == undefined ? "" : dts.fseName;
								var refusalReasons = dts.refusalReasons == undefined ? "" : dts.refusalReasons;
								var id = dts.id;
								var fseId = dts.fseId;
								if (state == 0) {
									state = "待接受"
								} else if (state == 1) {
									state = "已接受"
								} else if (state == 2) {
									state = "已签到"
								} else if (state == 3) {
									state = "已签离"
								} else if (state == 4) {
									state = "已完成"
								} else if (state == 5) {
									state = "自动签离"
								} else if (state == 6) {
									state = "已拒绝"
								}
								html += '<li>'
								html += '<div><span class="state">' + state + '</span>'
								html += '<span class="name">' + fseName + '</span>'
								html += '<img src="../../../images/icon_42.png" />'
								if(data.data.length > 1){
									html += '<span style="color:red" onclick="shanchu(\'' + fseId + '\')" >删除</span></div>'
								}
																
								if (state == "已拒绝") {
									html += '<div class="jjly"><span class="txt"><span style="color:#000;">拒绝理由：</span>' + refusalReasons + '</span></div>'
								}
								html += '</li>'
							}
							html += '</ul>'
							h("#yipaidan").html(html);
						} else {
							html = '<div class="nodata">—— 暂无数据 ——</div>';
							h("#yipaidan").html(html);
						}
					} else {
						var html = '<div class="nodata">—— 暂无数据 ——</div>';
						h("#yipaidan").html(html);
						app.toast(data.msg);
					}
				}, function(errorMsg) {
					var html = '<div class="nodata">—— 暂无数据 ——</div>';
					h("#yipaidan").html(html);
					app.toast("数据获取失败，请检查网络连接");
				});
			}

			// 获取该服务单待派单的FSE列表信息接口
			function getToBeAppointedFse() {
				app.showWaiting('加载中...')
				var name = h("#fsename").val();
				ajaxGet(BASE_URL + "cc/location/getToBeAppointedFse", {
					"ServiceNoticeId": ServiceNoticeId,
					"TeamleaderId": app.getUser().enginnerId,
					"name": name
				}, function(data) {
					app.closeWaiting()
					if (data.code == 200) {
						var html = "";
						h("#daizhipai").html("");
						if (data.data && data.data.length > 0) {
							for (var i = 0; i < data.data.length; i++) {
								var dts = data.data[i];
								var fseId = dts.fseId == undefined ? "" : dts.fseId;
								var fseName = dts.fseName == undefined ? "" : dts.fseName;
								var address = dts.address == undefined ? "" : dts.address;
								var stateCount = dts.stateCount == undefined ? 0 : dts.stateCount;
								// var distance = dts.distance == undefined ? 0 : dts.distance;
								var distance = dts.distance
								if(distance && distance!== "-"){
									distance = (distance / 1000).toFixed(1) + 'KM'
								}else {
									distance = ''
								}
								html += '<div class="zp_list autoHeight">'
								html += '<div class="clum" id="' + fseId + '" fseName="' + fseName + '">'
								html += '<div class="mui-input-row mui-checkbox zp_check mui-left">'
								html += '<input name="checkbox1" value="' + fseId + '" type="checkbox">'
								html += '<label>' + fseName + '</label>'
								html += '</div>'
								html += '<span class="num">' + stateCount + '</span>'
								html += '</div>'
								html += '<div class="clum"  onclick="geren(\'' + fseId + '\')"  >'
								html += '<span class="dan">' + distance  +'</span>'
								html += '<span class="address">' + address + '</span>'
								html += '</div>'
								html += '</div>';

							}
							h("#daizhipai").html(html);
						} else {
							html = '<div class="nodata">—— 暂无数据 ——</div>';
							h("#daizhipai").html(html);
						}
					} else {
						var html = '<div class="nodata">—— 暂无数据 ——</div>';
						h("#daizhipai").html(html);
						app.toast(data.msg);
					}
				}, function(errorMsg) {
					app.closeWaiting()
					var html = '<div class="nodata">—— 暂无数据 ——</div>';
					h("#daizhipai").html(html);
					app.toast("数据获取失败，请检查网络连接");					
				});
			}

			// 确定派工，不需要验证是否指定FSE的方法
			function quedingpg() {

				// 获取checkbox的值
				var ele = document.getElementsByName("checkbox1");
				fseIds = "";
				fseNames = "";
				for (var i = 0; i < ele.length; i++) {
					if (ele[i].checked) {
						fseIds += ele[i].value + ",";
						fseNames += document.getElementById(ele[i].value).getAttribute('fseName') + ",";
					}
				}
				if (fseIds == "") {
					app.closeWaiting()
					app.toast("请选择待指派人员");
					return
				}

				ajaxGet(BASE_URL + "cc/notice/ConfirmationDispatch", {
					"assignId": app.getUser().enginnerId,
					"ServiceNoticeId": ServiceNoticeId,
					"serviceNo": serviceNo,
					"EnginnerIds": fseIds,
					"EnginnerNames": fseNames
				}, function(data) {
					app.closeWaiting()
					if (data.code == 200) {
						app.toast("操作成功");

						// 关闭弹窗
						mui("#popover").popover('hide', document.getElementById("div"));

						// 查找上级页面并关闭页面
						var details = plus.webview.getWebviewById('view/service/assign/details.html');
						if (details) {
							details.close()
						}
						// 查找需要跳转的页面，并指定刷新的方法
						var assignList = plus.webview.getWebviewById('view/service/assign/assignList.html');
						if (assignList) {
							assignList.evalJS('getList()')
						}
						var toBeAssignedList = plus.webview.getWebviewById('view/service/notice/toBeAssignedList.html');
						if (toBeAssignedList) {
							toBeAssignedList.evalJS('initData()')
						}
						mui.back()
					} else {
						app.toast("操作失败");
					}
				}, function(errorMsg) {
					app.closeWaiting()
					console.log(JSON.stringify(errorMsg))
				});

			}

			function geren(fId) {
				app.jumpTo("/view/service/taskDetails/task_details.html", "view/service/taskDetails/task_details.html", {
					serviceNoticeId: ServiceNoticeId,
					serviceNo: serviceNo,
					enginnerId: fId,
					selfOrOther: 1
				});
			}

			function shanchu(fId) {
				fseId = fId;
				mui("#popover1").popover('toggle', document.getElementById("div1"));
			}
		</script>
		<!-- js代码 end -->
	</body>
</html>
